package com.fxp.project.jiuselu.web.models.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.models.order.entity.JslUserBalance;
import com.fxp.project.jiuselu.web.models.order.mapper.JslUserBalanceMapper;
import com.fxp.project.jiuselu.web.models.order.service.IJslUserBalanceService;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class JslUserBalanceImpl extends ServiceImpl<JslUserBalanceMapper, JslUserBalance> implements IJslUserBalanceService {

    @Resource
    private JslUserMapper jslUserMapper;

    @Override
    public void addBalance(Integer type, BigDecimal income,String orderId,Long benefitUser,Long createBy) {
        JslUser user =jslUserMapper.selectById(benefitUser);
        String imgUrl=user.getAvatar()==null?"":user.getAvatar();
        this.save(new JslUserBalance(type,income,user.getUserMoney(),createBy,user.getUserPhone(),orderId,benefitUser,imgUrl));
    }

    @Override
    public Page<JslUserBalance> findByUserId(Integer page,Integer pageSize,Integer type) {
        Long userId = Objects.requireNonNull(BaseController.getUser()).getUserId();
        LambdaQueryWrapper<JslUserBalance> eq;
        switch (type){
            //全部
            case 0:
                eq = new QueryWrapper<JslUserBalance>().lambda().eq(JslUserBalance::getBenefitUser, userId);
                break;
            //收入
            case 1:
                eq = new QueryWrapper<JslUserBalance>().lambda().eq(JslUserBalance::getBenefitUser, userId).ne(JslUserBalance::getType,3);
                break;
            //提现
            case 2:
                eq = new QueryWrapper<JslUserBalance>().lambda().eq(JslUserBalance::getBenefitUser, userId).eq(JslUserBalance::getType,3);
                break;
            default:
                throw new IllegalStateException("type不能为空: " + type);
        }

        List<JslUserBalance> dtoList = this.list(eq.orderByDesc(JslUserBalance::getCreateTime));
        int size = dtoList.size();
        List<JslUserBalance> list = dtoList.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());

        Page<JslUserBalance> result = new Page<>();
        result.setCurrent(page);
        result.setPages(pageSize);
        result.setTotal(size);
        result.setRecords(list);

        return result;
//        return new Page<JslUserBalance>(page, pageSize).setRecords(dtoList).setCurrent(dtoList.size());
    }
}
